const { ADD, MINUS } = require("./constant");

// 初始化的数据
const initialState = {
  // 存储数据的对象
  counter: 10,
};

// 定义一个 reducer 函数
function reducer(state = initialState, action) {
  const { counter } = state;
  const num = action.payload || 1;
  switch (action.type) {
    case ADD:
      return { ...state, counter: counter + num };
    case MINUS:
      return { ...state, counter: Math.max(0, counter - num) };
    default:
      return state;
  }
}

module.exports = reducer;
